package com.basics.common.utils.bean;


import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class BeanUtils {
    private static final int BEAN_METHOD_PROP_INDEX = 3;
    private static final Pattern GET_PATTERN = Pattern.compile("get(\\p{javaUpperCase}\\w*)");
    private static final Pattern SET_PATTERN = Pattern.compile("set(\\p{javaUpperCase}\\w*)");

    public BeanUtils() {
    }

    public static void copyBeanProp(Object dest, Object src) {
        List<Method> destSetters = getSetterMethods(dest);
        List srcGetters = getGetterMethods(src);

        try {
            Iterator var4 = destSetters.iterator();

            while(var4.hasNext()) {
                Method setter = (Method)var4.next();
                Iterator var6 = srcGetters.iterator();

                while(var6.hasNext()) {
                    Method getter = (Method)var6.next();
                    if (isMethodPropEquals(setter.getName(), getter.getName()) && setter.getParameterTypes()[0].equals(getter.getReturnType())) {
                        setter.invoke(dest, getter.invoke(src));
                    }
                }
            }
        } catch (Exception var8) {
            var8.printStackTrace();
        }

    }

    public static List<Method> getSetterMethods(Object obj) {
        List<Method> setterMethods = new ArrayList();
        Method[] methods = obj.getClass().getMethods();
        Method[] var3 = methods;
        int var4 = methods.length;

        for(int var5 = 0; var5 < var4; ++var5) {
            Method method = var3[var5];
            Matcher m = SET_PATTERN.matcher(method.getName());
            if (m.matches() && method.getParameterTypes().length == 1) {
                setterMethods.add(method);
            }
        }

        return setterMethods;
    }

    public static List<Method> getGetterMethods(Object obj) {
        List<Method> getterMethods = new ArrayList();
        Method[] methods = obj.getClass().getMethods();
        Method[] var3 = methods;
        int var4 = methods.length;

        for(int var5 = 0; var5 < var4; ++var5) {
            Method method = var3[var5];
            Matcher m = GET_PATTERN.matcher(method.getName());
            if (m.matches() && method.getParameterTypes().length == 0) {
                getterMethods.add(method);
            }
        }

        return getterMethods;
    }

    public static boolean isMethodPropEquals(String m1, String m2) {
        return m1.substring(3).equals(m2.substring(3));
    }
}

